$(function () {
//	$("#btn-search").on("click",function () {
//		$("#current").val('1')
//		$("#page-form").submit();
//	})
	loadMenu();
	loadMsg();
	window.setInterval(function(){
		loadMsg();
	 }, 5000);
	
	$("#admin-msg a[data-toggle='tooltip']").click(function(){
		showMsgTip();
	});
	
	var isTabs = true;
    var tabs = $("#content-wraper").tabs({
      init: [{
        iframe: true,
        content: "main",
        title: "首页",
        url: "/portal/iframe/index"
      }],
      refresh: false,
      draggable: false,
      preventDefault:false,
      navBar: {
        maxTabs: isTabs ? 30 : 0,
        maxTitleLength: 20
      },
      language: {
        navBar: {
          title: "页",
          showActivedTab: "显示当前页面",
          showRefreshTab: "刷新当前页面",
          closeAllTabs: "关闭全部页面",
          closeOtherTabs: "关闭其他页面"
        }
      }
    });
    //设置右侧导航高度
    $(".mainnav-menu-wrap").css({
      height: $("#mainnav").height() - $(".navbar-header").height()
    });
    //tooltip显示
    $('[data-toggle="tooltip"]').tooltip();
    //满屏
    $("#fullScreen").click(function () {
      if ($(this).data("isOpen") == "true") {
        $(this).data("isOpen", "false");
        $(this).find("i").addClass("ion-arrow-expand").removeClass("ion-arrow-shrink");
        $(this).attr("data-title", "全屏");
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else {
          if (document.msExitFullscreen) {
            document.msExitFullscreen()
          } else {
            if (document.mozCancelFullScreen) {
              document.mozCancelFullScreen()
            } else {
              if (document.webkitCancelFullScreen) {
                document.webkitCancelFullScreen()
              }
            }
          }
        }
      } else {
        $(this).data("isOpen", "true");
        $(this).find("i").addClass("ion-arrow-shrink").removeClass("ion-arrow-expand");
        $(this).attr("data-title", "关闭全屏");
        var c = document.documentElement;
        if (c.requestFullscreen) {
          c.requestFullscreen()
        } else {
          if (c.msRequestFullscreen) {
            c.msRequestFullscreen()
          } else {
            if (c.mozRequestFullScreen) {
              c.mozRequestFullScreen()
            } else {
              if (c.webkitRequestFullScreen) {
                c.webkitRequestFullScreen()
              }
            }
          }
        }
      }
      return false;
    });

   $("#mainnav-menu > .navItem li").click(function () {
       refreshTab();
       $("#content-wraper").ajaxSuccess(function(evt, request, settings) {
           $(this).append("<li>请求成功</li>");
       });
   });
   
   window.addEventListener('message', function (e) {
		var data = e.data;
		if (data && data.type && data.type == 'chainLink') { //判断一下
			parent.$(parent.document).data("tabs").create({
				iframe: "true",
				content: "info",
				title: data.title,
				url: data.url
			}, true);
		}
	}, false);
});

function loadMenu() {
	$.ajax({
        type : "POST",
        url : "/portal/menu/getMenuTree",
        async : false,
        contentType : "application/json",
        success : function(data, statuText, responseObj) {
        	if(data.code == 0){
        		var menus = data.data.menus
        		var str = "";
        		$.each(menus, function (i, v) {
        			str += '<li class="navItem">';
        			str += '<a href="javascript:" aria-expanded="false"> ' +
								'<i class="iconfont '+v.icon+' side-icons"></i> ' +
								'<span class="menu-title">'+v.name+'</span> <i class="arrow"></i>' +
        					'</a>';
        			
        			str += '<ul class="collapse">';
        			
        			$.each(v.meuns, function (j, m) {
        				str += '<li>';
        				str += '<a href="javascript:" data-iframe="true" data-url="'+m.url+'" class="tabsItem">'+m.name+'</a>';
        				str += '</li>';
        			});
        			str += '</ul>';
        			str += '</li>';
        		});
        		
        		$("#mainnav-menu").append(str);
        	}
        },
        error:function(xhr, textStatus){
	        var status = xhr.readyState;
	        if(status==0){
	        	layer.msg("网络异常，请稍后再试");
	        }else if(status==4){
	        	
	        }
        }
	});
}

function closeMsgTip(){
    $(".float-login-modal").animate(
        {bottom:-$(".float-login-modal").outerHeight()},
        {
            easing:"swing",
            duration:1000,
            complete:function(){
            }
        }
    );
};

function showMsgTip(){
	$(".float-login-modal").css({bottom:-$(".float-login-modal").outerHeight()})
    
    $(".float-login-modal").animate(
        {bottom:0},
        {
            easing:"swing",
            duration:1000
        }
    )
}

function loadMsg() {
	$.ajax({
        type : "POST",
        url : "/portal/msg/list",
        async : true,
        contentType : "application/json",
        success : function(data, statuText, responseObj) {
        	if(data.code == 0){
        		var b = $("#admin-msg").is(':visible');
        		var msgs = data.data.msgs;
        		if (msgs == 0) {
        			$("#admin-msg").hide();
        			return;
        		}
    			$("#admin-msg").show();
    			$("#admin-msg .badge").text(msgs > 99 ? "99+" : msgs);
        		
    			var lmtxt = "";
    			$.each(data.data.list, function (i, v) {
    				lmtxt += '<li><a href="javascript:;" data-iframe="true" data-title="'+v.name+'" data-url="'+v.url+'" class="tabsItem">'+v.text+'</a></li>';
    			});
    			
    			$("#admin-msg-list").html(lmtxt);
    			
    			if (!b) {
    				showMsgTip();
    			}
    			
        	}
        }
	});
}

//刷新ifram内容
function refreshTab() {
    setTimeout(function () {
        var currentId = $('.nav-tabs li.active').find('a').attr('data-id');
        var target = $('#'+currentId );
        var url = target.attr('src');
        target.attr('src', url).ready();
    },50)
}

//注销
var logout=function(o){
	var url=$(o).attr("data-url");
	sessionStorage.clear();
	window.location.href=url;
};